package com.synjones.pay.web.biz.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.synjones.pay.model.recharge.RechargeReqBean;
import java.io.IOException;
import java.net.URLDecoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author hsd
 */
public class RestFulClient {

    private static final Logger logger = LoggerFactory.getLogger(RestFulClient.class);
    private static final String CONTENT_TYPE = "application/json";
    
    public static String httpPost(String url,String json, String charSet){
        //post请求返回结果
        String str;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        try {
            if (null != json) {
                StringEntity entity = new StringEntity(json, charSet);
                entity.setContentEncoding(charSet);
                entity.setContentType(CONTENT_TYPE);
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            url = URLDecoder.decode(url, charSet);
            /**请求发送成功，并得到响应**/
            
            if (result.getStatusLine().getStatusCode() == 200) {
                    str = EntityUtils.toString(result.getEntity());
                    return str;
            }
            int statusCode = result.getStatusLine().getStatusCode();
            logger.info("httpPost状态码为【{}】",statusCode);
        } catch (IOException e) {
            logger.error("post请求提交失败:{}", url, e);
            return null;
        }
        return null;
    }
    
    public static Object restClient(Object obj){
         String json = "{\"age\":\"123\",\"name\":\"no zuo,no die.\",\"sex\":\"变态\"}";
         obj = JSON.parse(json);
        return obj;
    }
   
    public static void main(String[] args) {
//        String url = "http://peer1:8080/payBusiness/helloWorld";
//        TestBean bean = new TestBean();
//        bean.setAge("123");
//        bean.setName("no zuo,no die.");
//        bean.setSex("变态");
//        
//        String json toJSONString= JSON.toJSONString(bean, SerializerFeature.WriteMapNullValue, SerializerFeature.SortField);
//        System.out.println("提交json = " +json);
//        String result =  httpPost(url, json, "UTF-8");
//        System.out.println("result = " + result);
//          TestBean bean = (TestBean)restClient(new TestBean());
          RechargeReqBean bean = new RechargeReqBean();
          bean.setAccName("胡世党");
          bean.setAmount("900");
          bean.setRechargeAcc("99999");
          bean.setUcode("100001");
          bean.setRemark("我要测试");
          bean.setUname("清华大学");
          String json= JSON.toJSONString(bean, SerializerFeature.WriteMapNullValue, SerializerFeature.SortField);
          String s = RestFulClient.httpPost("http://localhost:8030/payment/recharge", json, "UTF-8");
    
    }
    
}
